package com.wjy.game.server;

import java.util.Objects;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;

/**
 * Hello world!
 *
 * @author Administrator
 */
@Slf4j
@SpringBootApplication
public class Server extends Application {

    public static void main(String[] args) {
        SpringApplication application =
            new SpringApplicationBuilder(Server.class).web(WebApplicationType.NONE).main(Server.class).build(args);
        application.run(args);
        log.info("启动成功");
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent home = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource("fxml/Home.fxml")));

        // 创建最小化和关闭按钮
        Button minimizeButton = new Button("_");
        minimizeButton.getStyleClass().add("control-button");
        minimizeButton.setOnAction(e -> primaryStage.setIconified(true));

        Button closeButton = new Button("X");
        closeButton.getStyleClass().add("control-button");
        closeButton.setOnAction(e -> primaryStage.close());

        // 将按钮放入 HBox 布局
        HBox buttonBox = new HBox(5, minimizeButton, closeButton);
        // 使用 Pane 让按钮靠右
        Pane spacer = new Pane();
        HBox topBar = new HBox(spacer, buttonBox);
        HBox.setHgrow(spacer, javafx.scene.layout.Priority.ALWAYS);

        // 将按钮布局和原内容放入 VBox 布局
        VBox root = new VBox(topBar, home);

        primaryStage.setTitle("五子棋");
        Scene scene = new Scene(root, 800, 500);
        // 加载 CSS 样式
        scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/css/home.css")).toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.TRANSPARENT);

        Image icon = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/icon.png")));
        primaryStage.getIcons().add(icon);

        primaryStage.show();
    }
}
